package com.qiaofang.applet.service.other.service.impl;

import com.qiaofang.applet.common.base.MongoBaseService;
import com.qiaofang.applet.service.other.domain.ShortCode;
import com.qiaofang.applet.service.other.repository.ShortCodeRepository;
import com.qiaofang.applet.service.other.service.ShortCodeService;
import com.qiaofang.applet.service.other.service.dto.GetCodeDto;
import com.qiaofang.applet.service.other.service.dto.GetUniqueUuidDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author daiyuxiang
 */
@Slf4j
@Service
@Deprecated
public class ShortCodeServiceImpl extends MongoBaseService<ShortCode, String> implements ShortCodeService {

    private final ShortCodeRepository shortCodeRepository;

    @Autowired
    public ShortCodeServiceImpl(ShortCodeRepository shortCodeRepository) {
        super(shortCodeRepository);
        this.shortCodeRepository = shortCodeRepository;
    }


    @Override
    public String getShortCode(String businessUniqueUuid, Integer businessType) {
        ShortCode shortCode = shortCodeRepository.findByBusinessUniqueUuidAndBusinessType(businessUniqueUuid, businessType);
        if (shortCode == null) {
            shortCode = insertShortCode(businessUniqueUuid, businessType);
        }
        return shortCode.getCode();
    }

    private ShortCode insertShortCode(String businessUniqueUuid, Integer businessType) {
        ShortCode shortCode = new ShortCode();
        shortCode.setBusinessType(businessType);
        shortCode.setBusinessUniqueUuid(businessUniqueUuid);
        String code = UUID.randomUUID().toString().replace("-", "").substring(0, 16);
        shortCode.setCode(code);
        try {
            shortCodeRepository.insert(shortCode);
        } catch (DuplicateKeyException e) {
            return insertShortCode(businessUniqueUuid, businessType);
        }
        return shortCode;
    }

    @Override
    public String getUniqueUuid(String code, Integer businessType) {
        ShortCode shortCode = shortCodeRepository.findByCodeAndBusinessType(code, businessType);
        if (shortCode == null) {
            return "";
        }
        return shortCode.getBusinessUniqueUuid();
    }

    @Override
    public List<String> getShortCodeList(List<GetCodeDto> getCodeDtoList) {
        if (CollectionUtils.isEmpty(getCodeDtoList)) {
            return null;
        }
        List<String> list = new ArrayList<>(getCodeDtoList.size());
        for (GetCodeDto getCodeDto : getCodeDtoList) {
            list.add(this.getShortCode(getCodeDto.getBusinessUniqueUuid(), getCodeDto.getBusinessType()));
        }
        return list;
    }

    @Override
    public List<String> getUniqueUuidList(List<GetUniqueUuidDto> getUniqueUuidDtoList) {
        if (CollectionUtils.isEmpty(getUniqueUuidDtoList)) {
            return null;
        }
        List<String> list = new ArrayList<>(getUniqueUuidDtoList.size());
        for (GetUniqueUuidDto getUniqueUuidDto : getUniqueUuidDtoList) {
            list.add(this.getUniqueUuid(getUniqueUuidDto.getCode(), getUniqueUuidDto.getBusinessType()));
        }
        return list;
    }
}
